<template>
	<view class="user-info">
		<form @submit="userSubmit">
			<button class="avatar-box" open-type="chooseAvatar" @chooseavatar="chooseavatar">
				<!-- <image class="avatar" :src="avatar" mode="aspectFill"></image> -->
				<u--image :src="avatar" width="128rpx" height="128rpx" shape="circle"></u--image>
				<view class="camera">
					<u-icon name="camera-fill" color="#000" size="32rpx"></u-icon>
				</view>
			</button>
			<view class="form-box">
				<view class="form-item">
					<view class="label">昵称</view>
					<input class="input" type="nickname" name="name" placeholder="请输入昵称" />
				</view>
				<!-- <view class="form-item">
					<view class="label">姓名</view>
					<input class="input" type="text" placeholder="请输入姓名" />
				</view>
				<view class="form-item">
					<view class="label">地区</view>
					<input class="input" type="text" placeholder="请选择" disabled />
				</view>
				<view class="form-item">
					<view class="label">手机号</view>
					<input class="input" type="text" placeholder="请输入" disabled />
					<button class="btn" size="mini" open-type="getPhoneNumber" @getphonenumber="getphonenumber">获取手机号</button>
				</view> -->
			</view>
			<!-- <view class="protocol-radio" :class="{ 'active': protocol }" @click="protocol = !protocol">
				<view class="checkbox"></view>
				<view class="text">
					<text>我已阅读并同意</text>
					<text class="link" @click.stop="goProtocol(2)">《用户协议》</text>
					<text>与</text>
					<text class="link" @click.stop="goProtocol(1)">《隐私条款》</text>
				</view>
			</view> -->
			<view class="button-box">
				<button class="btn" form-type="submit">保存</button>
			</view>
		</form>
	</view>
</template>

<script>
	import { uploadURL } from '@/api/config.js'
	import { changeUserInfo } from '@/api/api'

	export default {
		data () {
			return {
				// protocol: false,
				
				avatar: '',
				
				userForm: {
					avatar: '',
					name: '',
				}
			}
		},
		methods: {
			chooseavatar (e) {
				uni.showLoading({ mask: true })
				
				uni.uploadFile({
					url: uploadURL,
					filePath: e.detail.avatarUrl,
					name: 'img',
					success: (fileRes) => {
						uni.hideLoading({ noConflict: true })
						const data = JSON.parse(fileRes.data)
						if (data.code == 1) {
							this.avatar = data.path
							this.userForm.avatar = data.id
						} else {
							uni.showToast({
								icon: 'error',
								title: data.msg
							})
						}
					},
					fail: (fileErr) => {
						uni.hideLoading({ noConflict: true })
						uni.showToast({
							icon: 'error',
							title: '上传失败'
						})
					}
				})
			},
			userSubmit (e) {
				if (!this.userForm.avatar) {
					uni.showToast({
						title: '请选择头像',
						icon: 'none'
					})
					return
				}
				
				if (!e.detail.value.name) {
					uni.showToast({
						title: '请输入昵称',
						icon: 'none'
					})
					return
				}
			
				this.userForm.name = e.detail.value.name
			
				uni.showLoading({ mask: true })
				changeUserInfo(this.userForm).then(res => {
					if (res) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/my/my'
							})
						}, 200)
					}
				})
			},
			// getphonenumber (e) {
			// 	console.log(e.detail)
			// }
		}
	}
</script>

<style lang="scss">
.user-info {
	padding: 80rpx 30rpx 70rpx;
	.avatar-box {
		margin: 0 auto;
		padding: 0;
		width: 128rpx;
		height: 128rpx;
		border-radius: 50%;
		overflow: hidden;
		position: relative;
		.avatar {
			width: 100%;
			height: 100%;
		}
		.camera {
			position: absolute;
			bottom: 0;
			left: 0;
			right: 0;
			z-index: 10;
			height: 38rpx;
			background-color: rgba(255, 255, 255, 0.5);
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
	.form-box {
		margin-top: 96rpx;
		.form-item {
			height: 106rpx;
			display: flex;
			align-items: center;
			&:not(:last-child) {
				border-bottom: 1rpx solid #eee;
			}
			.label {
				width: 120rpx;
				color: #333333;
				font-size: 32rpx;
				font-weight: bold;
			}
			.input {
				flex: 1;
			}
			.btn {
				margin: 0 0 0 20rpx;
				padding: 0;
				border-radius: 10rpx;
				width: 140rpx;
				height: 58rpx;
				color: #fff;
				font-size: 20rpx;
				line-height: 58rpx;
				background-color: #5749f7;
			}
		}
	}
	.protocol-radio {
		margin-top: 62rpx;
	}
	.button-box {
		margin-top: 200rpx;
		padding: 0 44rpx;
		.btn {
			height: 88rpx;
			border-radius: 88rpx;
			color: #fff;
			font-size: 32rpx;
			font-weight: bold;
			line-height: 88rpx;
			background-color: #5749f7;
		}
	}
}
</style>
